package com.jack.bookshop.servlet;

import com.jack.bookshop.dao.UserDao;
import com.jack.bookshop.vo.ResultVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "UserSignupServlet", value = "/UserSignupServlet")
public class UserSignupServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String captcha = request.getParameter("captcha");
        String code = request.getSession().getAttribute("validate").toString();
        ResultVo validateResultVo = this.validate(username, password, password2, captcha, code);
        if(validateResultVo.getCode()==1) {
            ResultVo daoResultVo = new UserDao().signup(username, password);
            request.setAttribute("resultVo", daoResultVo);
            request.getRequestDispatcher("signup-result.jsp").forward(request, response);
        }else {
            request.setAttribute("resultVo", validateResultVo);
            request.getRequestDispatcher("signup-result.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected ResultVo validate(String username, String password, String password2, String captcha, String code) {
        ResultVo resultVo = new ResultVo();
        List<String> list = new ArrayList<String>();
        if(captcha!=null && code!=null && captcha.equalsIgnoreCase(code)) {
            if (username != null && username.length() <= 20 && username.length() >= 4) {
                // 通过
            }else {
                String s = "Username is too short or too long, it must be 4-20 characters long!";
                list.add(s);
            }
            if (password != null && password.length() <= 20 && password.length() >= 4) {
                // 通过
            }else {
                String s = "password is too short or too long, it must be 4-20 characters long!";
                list.add(s);
            }
            if (password != null && password2 !=null && password.equals(password2)) {
                // 通过
            }else {
                String s = "Please re-confirm your password. ";
                list.add(s);
            }
        }else {
            String s = "Captcha is not correct, Please try again!";
            list.add(s);
        }
        if(list.size()>0) {
            resultVo.fail();
        }else {
            resultVo.success();
        }
        resultVo.setList(list);
        System.out.println(resultVo);
        return resultVo;
    }
}
